﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;
using AjaxPro;
using System.Data.SqlClient;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem.ShangBao
{
    public partial class ShangBaoSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                BindGridView();
                DropDownListBind(string.Empty);
            }
        }

        #region DropDownListBind(x)
        public void DropDownListBind(string Deparatment)
        {
            //环评单位
            ddlhp.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.GetList();
            ddlhp.DataBind();
            ddlhp.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlhp.Items.Insert(1, new ListItem("全部", "全部"));
        }
        #endregion

        #region 按钮(搜索按钮、重置按钮)(x)
        //搜索按钮
        protected void lbtnSelect_Click(object sender, EventArgs e)
        {
            lbtnBind();
        }
        private void lbtnBind()
        {
            GridView1.DataSource = NoticeService.GetSelectss(txtProjectName.Text, ddlhp.SelectedValue, ddlShangbao.SelectedItem.Text);
            GridView1.DataBind();
        }
        //重置按钮
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            foreach (Control con in this.form1.Controls)
            {
                if (con is TextBox)
                    ((TextBox)con).Text = string.Empty;
                else if (con is CheckBox)
                    ((CheckBox)con).Checked = false;
                else if (con is DropDownList)
                    ((DropDownList)con).SelectedIndex = -1;
            }
        }
        #endregion

        #region GridView(x)
        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向   
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];
            // 调用业务数据获取方法   
            DataTable dtBind = this.getDB();
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图   
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            // GridView绑定并显示数据   
            this.GridView1.DataSource = dtBind;
            this.GridView1.DataBind();
        }
        // DataTable 数据源     
        private DataTable getDB()
        {
            DataTable dt = NoticeService.GetSelectAllShangBao();
            return dt;
        }
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;
                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }
        private void SortGridView(string sortExpression, string direction)
        {
            DataTable dt = getDB();
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + direction;
            GridView1.DataSource = dv; //将DataView绑定到GridView上
            GridView1.DataBind();
        }
        //处理GridView的排序事件
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (GridViewSortDirection == SortDirection.Ascending) //设置排序方向
            {
                GridViewSortDirection = SortDirection.Descending;
                SortGridView(sortExpression, " DESC");
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                SortGridView(sortExpression, " ASC");
            }
        }
        //鼠标滑动
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(GridView1);
        }
        #endregion

        #region 项目上报(x)
        protected void btnShangbao_Click(object sender, EventArgs e)
        {
            string IDlist = string.Empty;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)GridView1.Rows[i].Cells[0].FindControl("cbx");
                if (chk.Checked)
                    IDlist += chk.Value + ",";
            }
            if (IDlist != string.Empty)
            {
                Session["IDlist"] = IDlist.Trim(',');
                string aa = Session["IDlist"].ToString();
            }
            else
            {
                Session["IDlist"] = null;
            }
            if (Session["IDlist"] != null)
            {
                DevelopprojectbasicinfoDataCollection lst = BLL.HuanPingService.GetBySelectID(Session["IDlist"].ToString());
                foreach (DevelopprojectbasicinfoData item in lst)
                {
                    string ID = item.ID.ToString();//ID
                    string ProjectName = item.ProjectName; ;//项目名称

                    if (HuanPingService.AddHuanPing(ID, ProjectName) == true)
                        Jscript.Alert("项目上报成功！", this);
                    else
                        Jscript.Alert("项目上报失败！", this);
                    if (HuanPingService.Updatereportstate(ID, ProjectName) == true)
                    { }

                }
            }
            else
            {
                Jscript.Alert("请选择要上报的项目！", this);
            }
        }
        #endregion
    }
}